'use strict';
(function (exports) {
    var $ = null;
    var PlayerBackLayer = cc.Layer.extend({
        step: 0,
        round: 0,//第几轮  快进快退  退的是轮
        lastround: 0,
        isPause: false,
        onEnter: function () {
            cc.Layer.prototype.onEnter.call(this);
        },
        onExit: function () {
            cc.Layer.prototype.onExit.call(this);
            if (mRoom.interval) {
                clearInterval(mRoom.interval);
            }
            $ = null;
        },
        ctor: function () {
            this._super();
            var that = this;

            var scene = ccs.load(huzi.res.PhzPlayerBackLayer_json);
            this.addChild(scene.node);
            if (cc.winSize.width > 1280) {
                scene.node.setAnchorPoint(cc.p(0.5, 0.5));
                scene.node.setPosition(cc.p(cc.winSize.width * 0.5, cc.winSize.height * 0.5));
            }
            // loadNodeCCS(res.PhzPlayerBackLayer_json, this);
            $ = create$(this.getChildByName('Layer'));

            this.btn_backward = $('root.backward');
            this.btn_backward2 = $('root.backward2');
            this.btn_back = $('root.back');
            this.btn_toward = $('root.toward');
            this.btn_toward2 = $('root.toward2');
            this.btn_pause = $('root.pause');
            this.btn_play = $('root.play');
            this.nextRound = $('root.nextRound');
            this.beforeRound = $('root.beforeRound');
            TouchUtil.setOnclickListener(this.btn_play, this.playReplay.bind(this));
            TouchUtil.setOnclickListener(this.btn_pause, this.pauseReplay.bind(this));
            TouchUtil.setOnclickListener(this.btn_back, this.exit.bind(this));
            TouchUtil.setOnclickListener(this.btn_backward, this.backward.bind(this));
            TouchUtil.setOnclickListener(this.btn_toward, this.toward.bind(this));
            TouchUtil.setOnclickListener(this.nextRound, this.nextPlayBack.bind(this));
            TouchUtil.setOnclickListener(this.beforeRound, this.beforePlayBack.bind(this));

            mRoom.interval = setInterval(this.run.bind(this), 400);

            this.btn_backward2.setVisible(false);
            this.btn_toward2.setVisible(false);

            this.lastround = mRoom.replayData.getLastRound();
            this.checkBtnVisible();
            return true;
        },
        playReplay: function () {
            this.btn_play.setVisible(false);
            this.btn_pause.setVisible(true);
            this.isPause = false;
        },
        pauseReplay: function () {
            this.btn_play.setVisible(true);
            this.btn_pause.setVisible(false);
            this.isPause = true;
        },
        backward: function () {
            if (this.round > 0) {
                this.parent.clearRoom();
                this.pauseReplay();
                this.round--;
                var playerscards = mRoom.replayData.getAllPlayerCardsByStep(this.round);
                this.step = mRoom.replayData.getStepByRound(this.round);
                this.step = parseInt(this.step) - 1;
                this.resetPlayerCards(playerscards);
            }
            this.setBackTowardStatus();
        },
        toward: function () {
            if (this.round < this.lastround) {
                this.parent.clearRoom();
                this.pauseReplay();
                this.round++;
                var playerscards = mRoom.replayData.getAllPlayerCardsByStep(this.round);
                this.step = mRoom.replayData.getStepByRound(this.round);
                this.step = parseInt(this.step) - 1;
                this.resetPlayerCards(playerscards);
            }
            this.setBackTowardStatus();
            if (this.round == this.lastround) {
                this.isPause = false;
            }
        },
        beforePlayBack: function () {
            if (mRoom.curRound <= 1) {
                return;
            }
            clearInterval(mRoom.interval);
            network.start();
            this.step = 0;
            this.parent.nextRoundPlay(false);
        },
        nextPlayBack: function () {
            if (mRoom.curRound >= mRoom.rounds) {
                return;
            }
            clearInterval(mRoom.interval);
            network.start();
            this.step = 0;
            this.parent.nextRoundPlay(true);
        },
        resumeReplay: function () {
            this.checkBtnVisible();
            mRoom.interval = setInterval(this.run.bind(this), 400);
        },
        checkBtnVisible: function () {
            if (mRoom.curRound <= 1) {
                this.beforeRound.setVisible(false);
            } else {
                this.beforeRound.setVisible(true);
            }
            if (mRoom.curRound >= mRoom.rounds) {
                this.nextRound.setVisible(false);
            } else {
                this.nextRound.setVisible(true);
            }
        },
        setBackTowardStatus: function () {
            if (!this.btn_toward) {
                return;
            }
            if (this.round == 0) {
                this.btn_toward.setVisible(true);
                this.btn_toward2.setVisible(false);
                this.btn_backward.setVisible(false);
                this.btn_backward2.setVisible(true);
            } else if (this.round == this.lastround) {
                this.btn_toward.setVisible(false);
                this.btn_toward2.setVisible(true);
                this.btn_backward.setVisible(true);
                this.btn_backward2.setVisible(false);
            } else {
                this.btn_toward.setVisible(true);
                this.btn_toward2.setVisible(false);
                this.btn_backward.setVisible(true);
                this.btn_backward2.setVisible(false);
            }
        },
        setData: function (parent) {
            if (parent) this.parent = parent;
        },
        resetPlayerCards: function (playerscards) {
            for (var id in playerscards) {
                var playercard = playerscards[id];
                var pos = this.parent.getUserPosIndex(id);
                var outCards = this.parent['out' + pos];
                var openCards = this.parent['open' + pos];
                if (mRoom.shouxing && mRoom.sxUserId > 0) {
                    if (pos == 3)
                        continue;
                }

                if (pos == 0) {
                    mRoom['CardList'] = playercard.cards;
                    var cardList = mCard.getCardList();
                    this.parent.cardList = cardList;
                    this.parent.setupCards();
                } else if (pos == 1) {
                    mRoom['CardList1'] = playercard.cards;
                    this.parent['cardList1'] = mCard.getOtherCardList(mRoom['CardList1']);
                    this.parent.setupOtherCards(1);
                } else if (pos == 2) {
                    mRoom['CardList2'] = playercard.cards;
                    this.parent['cardList2'] = mCard.getOtherCardList(mRoom['CardList2']);
                    this.parent.setupOtherCards(2);
                } else {
                    mRoom['CardList3'] = playercard.cards;
                    this.parent['cardList3'] = mCard.getOtherCardList(mRoom['CardList3']);
                    this.parent.setupOtherCards(3);
                }
                for (var j = 0; j < playercard.out.length; j++) {
                    var card = playercard.out[j];
                    outCards.addCard(card);
                }
                for (var j = 0; j < playercard.open.length; j++) {
                    if (cc.sys.isObjectValid(openCards) && openCards.addCards)
                        openCards.addCards(playercard.open[j].cards, playercard.open[j].typ, false);
                }
                //设置胡息
                this.parent.updateHX(pos);
                this.parent.txtCardCount.setString(mAction.leftCardCount);
            }
        },
        exit: function () {
            showMessageBox('是否要退出当前回放?',
                function () {
                    mRoom.isReplay = false;
                    mRoom.shouxing = false;
                    gameData.deskPlayers = [];
                    mRoom.sxUserId = 0;
                    // 清理牌信息。
                    mRoom['CardList'] = [];
                    clearInterval(mRoom.interval);
                    network.start();
                    if (!gameData.InClubZhanjiLayer)
                        mRoom.club_id = 0;
                    // cc.director.runScene(new MainScene(null, mRoom.club_id));
                    showBoard('HallBoard');
                },
                false
            );
        },
        run: function () {
            if (this.isPause) {
                return;
            }
            this.step++;
            if (mRoom.replayData.isOver(this.step)) {
                clearInterval(this.interval);
            } else {
                this.round = mRoom.replayData.getRound(this.step);
                mRoom.replayData.runEvent(this.step);
            }
            this.changeTxtStep();
            this.setBackTowardStatus();
        },
        changeTxtStep: function () {
            var len = mRoom.replayData.getDataLength();
            var percent = Math.floor(this.step * 10000 / len) / 100.0;
        }

    });
    exports.PlayerBackLayer = PlayerBackLayer;
})(window);